package com.spring.schedule_system.dengxiaojiao.util;

import com.szlanyou.common.constants.CommonError;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @auther y
 * @date 2023/04/13
 */
public class LocalCheckParamUtil {
    private static String regexPhone="^1\\\\d{10}$|^(0\\\\d{2,3}-?|\\\\(0\\\\d{2,3}\\\\))?[1-9]\\\\d{4,7}(-\\\\d{1,8})?$";
    private static String regexMail="^(\\\\w+([-.][A-Za-z0-9]+)*){3,18}@\\\\w+([-.][A-Za-z0-9]+)*\\\\.\\\\w+([-.][A-Za-z0-9]+)*$";
    private static String regexIdCar="^([1-6][1-9]|50)\\d{4}(18|19|20)\\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$";

    /**
     * 检查不为空或不为空值
     *
     * @param o
     * @param fields
     * @param <T>
     * @return
     */
    public static <T> void checkObjReqParams(T o, Map<String,String> fields,String msgContent) {
        StringBuilder sb = new StringBuilder("");
        for (String field : fields.keySet()) {
            try {
                Field f = o.getClass().getDeclaredField(field);
                f.setAccessible(true);
                Object o1 = f.get(o);
                if (isEmpty(o1)) {
                    sb.append(fields.get(field)).append(",");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1);
            throw BizException.newBizException(CommonError.PARAM_NULL.getErrorCode(), msgContent + CommonError.PARAM_NULL.getMsg() + " - " + sb.toString());
        }
    }
    /**
     * 检查不为空或不为空值
     *
     * @param o
     * @param fields
     * @param <T>
     * @return
     */
    public static <T> void checkObjReqParams(T o, Map<String,String> fields) {
        StringBuilder sb = new StringBuilder("");
        for (String field : fields.keySet()) {
            try {
                Field f = o.getClass().getDeclaredField(field);
                f.setAccessible(true);
                Object o1 = f.get(o);
                if (isEmpty(o1)) {
                    sb.append(fields.get(field)).append(",");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1);
            throw BizException.newBizException(CommonError.PARAM_NULL.getErrorCode(), CommonError.PARAM_NULL.getMsg() + " - " + sb.toString());
        }
    }

    /**
     * 正则校验
     * @param str
     * @param mode
     */
    public static void checkRegex(String str,String mode){
        if (!regexMatch(str,mode)){
            throw BizException.newBizException(CommonError.PARAM_ILLEGAL.getErrorCode(), CommonError.PARAM_ILLEGAL.getMsg() + " - " + str +"格式不正确");
        }
    }
    private static Boolean isEmpty(Object obj) {
        if (obj == null) {
            return true;
        } else if ("".equals(obj)) {
            return true;
        } else if ("null".equals(obj)) {
            return true;
        } else {
            if (obj instanceof String) {
                if (((String)obj).length() == 0) {
                    return true;
                }
            } else if (obj instanceof Collection) {
                if (((Collection)obj).size() == 0) {
                    return true;
                }
            } else if (obj instanceof Map && ((Map)obj).size() == 0) {
                return true;
            }

            return false;
        }
    }

    private static boolean regexMatch(String str,String mode){
        String patternStr="";
        switch (mode){
            case "phone":
                patternStr=regexPhone;
                break;
            case "mail":
                patternStr=regexMail;
                break;
            case "idCard":
                patternStr=regexIdCar;
                break;
            default:
                break;
        }
        if (patternStr.length()<=0){
            return false;
        }
        Pattern pattern = Pattern.compile(patternStr);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }
}
